package com.campus.payment.controller;

import com.campus.common.bean.common.Resp;
import com.campus.payment.dto.ProfitSharingRequestDTO;
import com.campus.payment.dto.ProfitSharingResponseDTO;
import com.campus.payment.service.ProfitSharingService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * 分账控制器
 *
 * @author ZhaoYuJie
 * @since 2025-01-15
 */
@Slf4j
@RestController
@RequestMapping("/profit-sharing")
@Api(tags = "分账管理")
public class ProfitSharingController {

    @Autowired
    private ProfitSharingService profitSharingService;

    /**
     * 添加分账接收方
     */
    @PostMapping("/add-receiver")
    @ApiOperation("添加分账接收方")
    public Resp<Boolean> addReceiver(
            @ApiParam("子商户号") @RequestParam String subMchid,
            @ApiParam("接收方类型") @RequestParam String type,
            @ApiParam("接收方账号") @RequestParam String account,
            @ApiParam("接收方名称") @RequestParam(required = false) String name,
            @ApiParam("关系类型") @RequestParam String relationshipType) {
        try {
            boolean success = profitSharingService.addReceiver(subMchid, type, account, name, relationshipType);
            return Resp.success(success);
        } catch (Exception e) {
            log.error("添加分账接收方失败", e);
            return Resp.error("添加分账接收方失败：" + e.getMessage());
        }
    }

    /**
     * 请求分账
     */
    @PostMapping("/create")
    @ApiOperation("请求分账")
    public Resp<ProfitSharingResponseDTO> createProfitSharing(
            @ApiParam("分账请求") @RequestBody ProfitSharingRequestDTO request) {
        try {
            ProfitSharingResponseDTO response = profitSharingService.createProfitSharing(request);
            return Resp.success(response);
        } catch (Exception e) {
            log.error("请求分账失败", e);
            return Resp.error("请求分账失败：" + e.getMessage());
        }
    }

    /**
     * 查询分账结果
     */
    @GetMapping("/query")
    @ApiOperation("查询分账结果")
    public Resp<ProfitSharingResponseDTO> queryProfitSharing(
            @ApiParam("子商户号") @RequestParam String subMchid,
            @ApiParam("微信支付订单号") @RequestParam String transactionId,
            @ApiParam("商户分账单号") @RequestParam String outOrderNo) {
        try {
            ProfitSharingResponseDTO response = profitSharingService.queryProfitSharing(subMchid, transactionId, outOrderNo);
            return Resp.success(response);
        } catch (Exception e) {
            log.error("查询分账结果失败", e);
            return Resp.error("查询分账结果失败：" + e.getMessage());
        }
    }
}